package xyz.xmh.pattern.behavioral.iteratorpattern;

/**
 * @author Administrator
 * 实现容器接口的实体类，该类有实现了 Iterator 接口的内部类 NameIterator。
 */
public class NameRepository implements Container {

    public String[] names = {"Robert", "John", "Julie", "Lora"};

    @Override
    public Iterator getIterator() {
        return this.new NameIterator();
    }

    private class NameIterator implements Iterator {

        int index;

        @Override
        public boolean hasNext() {
            return index < names.length;
        }

        @Override
        public Object next() {
            if (this.hasNext()) {
                return names[index++];
            }
            return null;
        }
    }

}
